package com.seawaterbt.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seawaterbt.ssm.config.DataSource;
import com.seawaterbt.ssm.config.DataSourceEnum;
import com.seawaterbt.ssm.entity.*;
import com.seawaterbt.ssm.mapper.ShengDatHeatStationDataMapper;
import com.seawaterbt.ssm.service.CzBasHeatstationService;
import com.seawaterbt.ssm.service.CzDatHeatstationdataService;
import com.seawaterbt.ssm.service.ShengDatHeatStationDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhf
 * @since 2019-10-31
 */
@Service
public class ShengDatHeatStationDataServiceImpl extends ServiceImpl<ShengDatHeatStationDataMapper, ShengDatHeatStationData> implements ShengDatHeatStationDataService {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    CzDatHeatstationdataService heatstationdataService;
    @Autowired
    CzBasHeatstationService basHeatstationService;

    @DataSource(DataSourceEnum.DB1)
    @Override
    public boolean saveOrUpdate(ShengDatHeatStationData entity) {
        return super.saveOrUpdate(entity);
    }

    @DataSource(DataSourceEnum.DB1)
    @Override
    public ShengDatHeatStationData getByCodeTime(QueryWrapper wrapper) {
        return this.getOne(wrapper);
    }

    @DataSource(DataSourceEnum.DB1)
    @Override
    public void sendData() {
        QueryWrapper<CzDatHeatstationData> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("heatstation_code", 130981011).or().likeRight("heatstation_code", 130903007);
        Date now = new Date();
        List<CzDatHeatstationData> czDatHeatstationData = heatstationdataService.list(queryWrapper);
        for (CzDatHeatstationData czDatCrew : czDatHeatstationData) {
            CzBasHeatstation one = basHeatstationService.listOne(czDatCrew.getHeatstationCode());
            if (one != null && one.getCodeProvince() != null && !one.getCodeProvince().equals("")) {
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("HeatStationID", one.getCodeProvince());
                wrapper.eq("GetTime", czDatCrew.getGetTime());
                ShengDatHeatStationData byCodeTime = getByCodeTime(wrapper);
                if (byCodeTime == null) {
                    ShengDatHeatStationData shengData = new ShengDatHeatStationData();
                    shengData.setHeatStationId(one.getCodeProvince());
                    shengData.setAddTime(now);
                    shengData.setGetTime(czDatCrew.getGetTime());
                    shengData.setBackPre(Double.valueOf(czDatCrew.getBackPre()));
                    shengData.setBackTemp(Double.valueOf(czDatCrew.getBackTemp()));
                    shengData.setCumulativeHeat(Double.valueOf(czDatCrew.getCumulativeHeat()));
                    shengData.setInstantaneousFlow(Double.valueOf(czDatCrew.getInstantaneousFlow()));
                    shengData.setSupplyPre(Double.valueOf(czDatCrew.getSupplyPre()));
                    shengData.setSupplyTemp(Double.valueOf(czDatCrew.getSupplyTemp()));
                    this.save(shengData);
                }
            }
        }

        System.out.println("完成同步换热站到省平台 " + df.format(new Date()));
    }

    @DataSource(DataSourceEnum.DB1)
    @Override
    public boolean save(ShengDatHeatStationData entity) {
        return super.save(entity);
    }
}
